package com.wmg.utils;

import org.springframework.util.DigestUtils;

public class PasswordUtil {
    public static String getshortPass(String oldpassword, String newPasswordLength) {
        //MD5加密后的字符串为
        String encodeStr = DigestUtils.md5DigestAsHex(oldpassword.getBytes());

        boolean flag = true;
        char[] encode = encodeStr.toCharArray();
        StringBuilder password = new StringBuilder();

        //得到字母数字交替的字符串pass=b2d7a9f4e9;
        boolean isDigit = Character.isDigit(encode[0]);
        int index = 0;

        while (index < encode.length) {
            if (Character.isDigit(encode[index]) && flag) {
                password.append(encode[index]);
                flag = !flag;
            }
            if (!Character.isDigit(encode[index]) && !flag) {
                flag = !flag;
                password.append(encode[index]);
            }
            index++;
        }
        System.out.println(password);

        //压缩n位
        int n = Integer.valueOf(newPasswordLength) - 4;

        int length = password.length();
        if (length > n) {
            int begin = (length - n) / 2;
            int end = begin + n;
            password = new StringBuilder(password.substring(begin, end));
            System.out.println(password);

        } else {
            int low = n - length;
            while (low-- > 0) {
                password.append("0");
            }
        }
        //大写字母
        char[] newpass = password.toString().toCharArray();
        //统计字母个数
        int count = 0;
        for (int i = 0; i < newpass.length; i++) {
            if (!Character.isDigit(newpass[i])) {
                count++;
            }
        }
        //对其中一半字母转大写
        index = 0;
        count = count > 1 ? count / 2 : 1;
        while (index < newpass.length && count > 0) {
            if (!Character.isDigit(newpass[index])) {
                newpass[index] = Character.toUpperCase(newpass[index]);
                count--;
            }
            index++;
        }
        System.out.println(newpass);

        //增加符号^ $..++
        String res = "^" + new String(newpass) + ".+$";
        System.out.println(res);
        return res;
    }



    /*public static void main(String[] args) {
        //加密后的字符串
        String string = "str";
        String encodeStr = DigestUtils.md5DigestAsHex(string.getBytes());
//                md5Hex(text + key);
        System.out.println("MD5加密后的字符串为:encodeStr=" + encodeStr);


        boolean flag = true;
        char[] encode = encodeStr.toCharArray();
        String pass = "";

        int[] num = new int[6];

//33955a11e22f11f55f33e33abdb5523175f447ee
//        int index = 0 ;
//        for (int i = 0; i < encode.length&&index<num.length; i++) {
//
//        }
//
        int index = 0;
        boolean isDigit = Character.isDigit(encode[0]);
//        if (!isDigit) {
//            pass += encode[0];
//        }
        while (index < encode.length) {
            if (Character.isDigit(encode[index]) && flag) {
                pass += encode[index];
                flag = !flag;
            }
            if (!Character.isDigit(encode[index]) && !flag) {
                flag = !flag;
                pass += encode[index];
            }
            index++;
        }
        System.out.println(pass);
        //得到字母数字交替的字符串pass=b2d7a9f4e9;

        //得到7位
        int length = pass.length();
        if (length > 7) {
            int begin = (length - 7) / 2;
            int end = begin + 7;
            pass = pass.substring(begin, end);
            System.out.println(pass);

        } else {
            int low = 7 - length;
            while (low-- > 0) {
                pass += "0";
            }
        }
        //大写字母
        char[] newpass = pass.toCharArray();
        //统计字母个数
        int count = 0;
        for (int i = 0; i < newpass.length; i++) {
            if (!Character.isDigit(newpass[i])) {
                count++;
            }
        }
        //对其中一半字母转大写
        index = 0;
        count = count > 1 ? count / 2 : 1;
        while (index < newpass.length && count > 0) {
            if (!Character.isDigit(newpass[index])) {
                newpass[index] = Character.toUpperCase(newpass[index]);
                count--;
            }
            index++;
        }

        System.out.println(newpass);

        //增加符号^ $..++
        String res = "^" + new String(newpass) + ".+$";
        System.out.println(res);


        //
        //^9F9C9f6..++$

    }*/
}
